package com.dianxin.smartpark.service.utils;


import com.chinatelecom.response.common.CommonResponseBody;
import com.dianxin.acl.api.acl.AclUserRpcService;
import com.dianxin.acl.api.qo.SsoUserRpcQO;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkGroupObject;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkGroupObjectExample;
import com.dianxin.smartpark.dao.mapper.attendance.SmartParkGroupObjectMapper;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkAttendanceRecordQO;
import com.dianxin.smartpark.service.attendance.SmartParkAttendanceRecordService;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间处理列
 *
 * @author chenyong
 * @since 2021-01-22
 */
@Service
@Lazy(false)
@Component
public class DateTimeUtil {

	@Resource
	private SmartParkAttendanceRecordService smartParkAttendanceRecordService;
	@Resource
	private SmartParkGroupObjectMapper smartParkGroupObjectMapper;
	@Resource
	private AclUserRpcService aclUserRpcService;
	/**
	 * 修改日期的时分
	 * @param date 日期
	 * @param hour 小时
	 * @param minute 分钟
	 * @return
	 */
	public static Date handle(Date date,int hour,int minute){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 将时分秒,毫秒域清零
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 00);
		return calendar.getTime();
	}

	/**
	 * 获得传入时间为是星期几
	 */
	public static int getWeekDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int weekDay = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (weekDay == 0){
			weekDay = 7;
		}
		return weekDay;
	}

	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0){
			w = 0;
		}
		return weekDays[w];
	}

	/**
	 * @Description:  判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 * @Author: ChenYong
	 * @Date: 2021/7/28 0028
	 * @param nowStr: 当前时间
	 * @param startStr: 开始时间
	 * @param endStr: 结束时间
	 * @Return: boolean
	 */
	public static boolean isEffectiveDate(String nowStr,String startStr,String endStr) {
		String format = "hh:mm";
		Date nowTime = null;
		Date startTime = null;
		Date endTime = null;
		try {
			nowTime = new SimpleDateFormat(format).parse(nowStr);
			startTime = new SimpleDateFormat(format).parse(startStr);
			endTime = new SimpleDateFormat(format).parse(endStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (nowTime.getTime() == startTime.getTime()
				|| nowTime.getTime() == endTime.getTime()) {
			return true;
		}
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);
		Calendar end = Calendar.getInstance();
		end.setTime(endTime);
		//判断当前时间大于开始时间并且小于结束时间
		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据传入时分修改时间时分
	 * @Description:  根据传入时分修改时间时分
	 * @Author: ChenYong
	 * @Date: 2021/8/5 0005
	 * @param time:
	 * @Return: Date
	 */
	public static Date update(Date date,String time){
		String format = "HH:mm";
		try {
			date = new SimpleDateFormat(format).parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		Date handle = DateTimeUtil.handle(new Date(), hour, minute);
		return handle;
	}

	/**
	 * A获取某段时间内的所有日期
	 * @Description:
	 * @Author: ChenYong
	 * @Date: 2021/10/8 0008
	 * @param dStart:
	 * @param dEnd:
	 * @Return: List<Date>
	 */
	public static List<Date> findDates(Date dStart, Date dEnd) {
		Calendar cStart = Calendar.getInstance();
		cStart.setTime(dStart);

		List dateList = new ArrayList();
		//别忘了，把起始日期加上
		dateList.add(dStart);
		// 此日期是否在指定日期之后
		while (dEnd.after(cStart.getTime())) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			cStart.add(Calendar.DAY_OF_MONTH, 1);
			dateList.add(cStart.getTime());
		}
		return dateList;
	}


	@Scheduled(cron = "0 50 23 * * ?")//每天23点触发，   周一至周五21点过5分触发  0 05 21 ? * MON-FRI
	private void automatic() {
	    SmartParkGroupObjectExample smartParkGroupObjectExample = new SmartParkGroupObjectExample();
		smartParkGroupObjectExample.or().andIsDeletedEqualTo(0);
		List<SmartParkGroupObject> smartParkGroupObjects = smartParkGroupObjectMapper.selectByExample(smartParkGroupObjectExample);
		for (SmartParkGroupObject smartParkGroupObject:smartParkGroupObjects){
			SmartParkAttendanceRecordQO smartParkAttendanceRecordQO = new SmartParkAttendanceRecordQO();

			SsoUserRpcQO qo = new SsoUserRpcQO();
			qo.setUserId(String.valueOf(smartParkGroupObject.getUserId()));
			CommonResponseBody<SsoUserRpcQO> userDetail = aclUserRpcService.getUserDetail(qo);

			if(userDetail.getData()!=null&&userDetail.getData().getDeptList().size()>0){
				smartParkAttendanceRecordQO.setDeptId(userDetail.getData().getDeptList().get(0).getId());
				smartParkAttendanceRecordQO.setDeptName(userDetail.getData().getDeptList().get(0).getDeptName());
				smartParkAttendanceRecordQO.setUserName(userDetail.getData().getNickName());
			}
			smartParkAttendanceRecordQO.setUserId(smartParkGroupObject.getUserId());
			smartParkAttendanceRecordQO.setTenantId(smartParkGroupObject.getTenantId());
			smartParkAttendanceRecordService.saveAbnormalAttendanceRecord(smartParkAttendanceRecordQO);
		}
	}

}
